package com.reset.password.controller;

import com.reset.password.entity.User;
import com.reset.password.entity.dto.UserDTO;
import com.reset.password.entity.dto.UserRegisterDTO;
import com.reset.password.entity.vo.UserVO;
import com.reset.password.result.Result;
import com.reset.password.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Abel
 * @since 2024-11-05
 */
@Tag(name = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;
    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result login(String email, String password){
        UserVO loginUser = userService.login(email,password);
        if (loginUser == null){
            return Result.error();
        }
        return Result.success(loginUser);
    }
    @Operation(summary = "用户注册")
    @PutMapping("/register")
    public Result register(@RequestBody UserRegisterDTO user){
        UserVO registerUser = userService.register(user);
        if (registerUser== null){
            return Result.error();
        }
        return Result.success(registerUser);
    }
    @Operation(summary = "修改密码")
    @PostMapping("/updatePassword")
    public Result updatePassword(String email, String password,String confirmPassword){
        UserDTO user = new UserDTO();
        user.setEmail(email);
        user.setPassword(password);
        boolean result = userService.updatePassword(user);
        if (!result){
            return Result.error();
        }
        return Result.success();
    }
    // 是否存在这个用户
    @Operation(summary = "是否存在这个用户")
    @GetMapping("/isExist/{email}")
    public Result isExist(@PathVariable String email){
        UserVO user = userService.findByEmail(email);
        if (user == null){
            return Result.error();
        }
        return Result.success(user);
    }
}
